import Vue from "vue"

import Vuex from "vuex"

import axios from "axios"

Vue.use(Vuex)

// 创建state对象
const state = {
  count: 1,
  price: "¥99",
  movieList: []
}

// 创建mutations对象
const mutations = {
  increment(state) {
    state.count++
  },
  decrement(state) {
    state.count--
  },
  incrementN(state, { n }) {
    state.count += n
  },
  setMovieList(state, movieList) {
    state.movieList = movieList
  }
}

// 创建actions对象
const actions = {
  waitIncrementN({ commit }, payload) {
    setTimeout(() => {
      commit("incrementN", payload)
    }, 1000);
  },

  async getMovieList({ commit }) {
    const result = await axios.get("https://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=11&page_id=2&ret_num=48&session=b9fd987164f6aa47fad266f57dffaa6a")
    commit("setMovieList", result.data.data.list)
  }
}

// 创建getters对象
const getters = {
  myMovieList(state) {
    return state.movieList.filter((movie) => {
      return movie.albumName.length < 7 && movie.albumName.length > 3
    })
  },
  yourMovieList(state) {
    return ({ start, end }) => {
      return state.movieList.filter((movie) => {
        return movie.albumName.length <= end && movie.albumName.length >= start
      })
    }
  }
}

export default new Vuex.Store({
  state,
  mutations,
  actions,
  getters
})